﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class mobile_ajaxdata_Customer : BaseDataPage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
         string aciton = Request.Params["aciton"];
         switch (aciton)
         {
             case "getmyshopmoney":
                 getmyshopmoney();
                 break;
             case "getmyshopdzlist":
                 getmyshopdzlist();
                 break;
             case "getmyshopdzdtl":
                 getmyshopdzdtl();
                 break;
             case "upshopstate":
                 upshopstate();
                 break;
             case "getmyshopsalelist":
                 getmyshopsalelist();
                 break;
             case "getmyshopexpenlist":
                 getmyshopexpenlist();
                 break;
             case "getaccount":
                 getaccount();
                 break;
             case "getmyshopsalesum":
                 getmyshopsalesum();
                 break;
             case "getmysalebyshop":
                 getmysalebyshop();
                 break;
         }
    }

    private void getmysalebyshop()
    {
        string userid = Request.Params["userid"];
        string shopid = Request.Params["shopid"];
        string bdate = Request.Params["bdate"];
        string edate = Request.Params["edate"];
        if (bdate == "" && edate == "")
        {
            bdate = DateTime.Now.AddDays(-8).ToString("yyyy-MM-dd");
            edate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
        }
        string str = " and saledate between '" + bdate + "' and '" + edate + "'";
        if (shopid != "0")
        {
            str += " and a.shopcode = '" + shopid + "'";
        }
        string sql = @"select  a.shopname,isnull(sum(RealPayAmount),0)as saleamt,isnull(sum(sjpayclamt),0)as fctot,
                                           isnull(sum(sjpayclamt2),0)as payed,isnull(sum(endAmount),0)as endamt
                                          from V_CustomerAccountDtl a
                                        inner join V_UserShopDataRights u on u.shopcode = a.ShopCode where userid='" + userid + "' "
                               + str + " group by a.shopname";
        GetDataToJsonBySql(Response, sql);

    }

    private void getmyshopsalesum()
    {
        string userid = Request.Params["userid"];
        string shopid = Request.Params["shopid"];
        string bdate =  Request.Params["bdate"];
        string edate =  Request.Params["edate"];
        if (bdate == "" && edate == "")
        {
            bdate = DateTime.Now.AddDays(-8).ToString("yyyy-MM-dd");
            edate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
        }
        string str = " and saledate between '"+bdate+"' and '"+edate+"'";
        if (shopid != "0")
        {
            str += " and a.shopcode = '"+shopid+"'";
        }
        object result = null;
        string sql=@"select  isnull(sum(RealPayAmount),0)as saleamt,isnull(sum(sjpayclamt),0)as fctot,
                                           isnull(sum(sjpayclamt2),0)as payed,isnull(sum(endAmount),0)as endamt
                                          from V_CustomerAccountDtl a
                                        inner join V_UserShopDataRights u on u.shopcode = a.ShopCode where userid='"+userid+"' " + str;
        //result = new { status = 1, msg = sql };
        //Response.Write(JsonHelper.SerializeObject(result));
        //Response.End();
        //return;

        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                result = new
                {
                    bdate = bdate,
                    edate = edate,
                    saleamt =string.Format("{0:0,00}",ds.Tables[0].Rows[0]["saleamt"]),
                    fctot = string.Format("{0:0,00}",ds.Tables[0].Rows[0]["fctot"]),
                    payed = string.Format("{0:0,00}",ds.Tables[0].Rows[0]["payed"]),
                    endamt = string.Format("{0:0,00}",ds.Tables[0].Rows[0]["endamt"]),
                };
            }
            else
            {
                result = new { status = 1, msg = "没有数据！" };
            }
        }
        else
        {
            result = new { status = 0, msg = "出现错误" };
        }
        Response.Write(JsonHelper.SerializeObject(result));
        Response.End();
    }

    private void getaccount()
    {
        string shopid = Request.Params["shopid"];
        object result = null;
        DataSet ds = mydb.DM_GetDateSet("select *from Bas_ShopBegin where shopcode ='"+shopid+"'");
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                result = new
                {
                    accountname = ds.Tables[0].Rows[0]["accountname"],
                    accountcode = ds.Tables[0].Rows[0]["accountcode"],
                    accountbank = ds.Tables[0].Rows[0]["accountbank"],
                };
            }
            else
            {
                result = new { status = 1, msg = "没有数据！" };
            }
        }
        else
        {
            result = new { status = 0, msg = "出现错误" };
        }
        Response.Write(JsonHelper.SerializeObject(result));
        Response.End();
    }

    private void getmyshopsalelist()
    {
        string sbr = "";
        string bdate = Request.Params["bdate"];
        string shopid = Request.Params["shopid"];
        string sql = "select convert(varchar(20),saledate,23)as saledate,realpayamount,fctot,fyamt from V_CustomerAccountDtl where shopcode = '" + shopid
            +"' and convert(varchar(7),saledate,23)='"+bdate+"' order by saledate";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count == 0)
            {
                sbr = "{\"status\":\"1\",\"msg\":\"没有数据！\"}";
            }
            else
            {
                sbr = DataSetToJson(ds);
            }
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }

    private void getmyshopexpenlist()
    {
        string sbr = "";
        string bdate = Request.Params["bdate"];
        string shopid = Request.Params["shopid"];
        string sql = @"select * into #temp from (
                        select expensename,sum(Total)as payamount from Bas_Stand_Expenses 
                        where convert(varchar(7),paydate,23)='" + bdate + "' and shopcode ='" + shopid + @"'
                        group by expensename
                        union all
                        select ExpenseName,sum(Total)as total from Bas_Shipping_Expenses
                        where convert(varchar(7),paymonth,23)='" + bdate + "' and shopcode ='" + shopid + @"'
                        group by expensename
                        union all
                        select ExpenseName,sum(Total)as total from Bas_Return_Expenses
                        where convert(varchar(7),paymonth,23)='" + bdate + "' and shopcode ='" + shopid + @"'
                        group by expensename
                        union all
                        select ExpenseName,sum(Total)as total from Bas_ShopMat_Expenses
                        where convert(varchar(7),paymonth,23)='" + bdate + "' and shopcode ='" + shopid + @"'
                        group by expensename
                        union all
                        select ExpenseName,sum(Total)as total from Bas_Salary_Expenses
                        where convert(varchar(7),paymonth,23)='" + bdate + "' and shopcode ='" + shopid + @"'
                        group by expensename
                        union all
                        select '垫支',sum(paymoney)as total from Bas_ShopExpens
                        where convert(varchar(7),PayDate,23)='" + bdate + "' and shopcode ='" + shopid + @"'
                        )m where isnull(payamount,0) >0
                        insert into #temp select '合计',sum(payamount) from #temp
                        select *from #temp drop table #temp    ";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count == 0)
            {
                sbr = "{\"status\":\"1\",\"msg\":\"没有数据！\"}";
            }
            else
            {
                sbr = DataSetToJson(ds);
            }
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }

    private void upshopstate()
    {
        string sbr = "";
        string userid = Request.Params["userid"];
        string bdate = Request.Params["bdate"];
        string shopid = Request.Params["shopid"];
        string detail = Request.Params["detail"];
        string sql = "update KM_CustomerMonthAcc set ClState =1,cldate=getdate(),remark='"
            + detail + "'  where shopcode='" + shopid + "' and convert(varchar(7),paydate,23)='" + bdate + "'";
        try
        {
            if (mydb.DM_ExecuteNonQuery(sql) != -1)
            {
                sbr = "{\"status\":\"1\",\"msg\":\"操作成功！\"}";
            }
            else
            {
                sbr = "{\"status\":\"0\",\"msg\":\"操作失败，请系统IT！\"}";
            }
        }
        catch (Exception ex)
        {
            sbr = "{\"status\":\"0\",\"msg\":\""+ex.Message+"\"}";
        }
        Response.Write(sbr);
        Response.End();
    }

    private void getmyshopdzdtl()
    {
        string shopid = Request.Params["shopid"];
        string bdate = Request.Params["bdate"];
        string sql = @"select * from v_CustomerMonthAcc
                        where shopcode='" + shopid + "' and paydate='" + bdate + "'";  
        GetDataToJsonBySql(Response,sql);
    }

    private void getmyshopdzlist()
    {
        string userid = Request.Params["userid"];
        string ClState = Request.Params["stype"];
        string pageindex = Request.Params["pageindex"];
        string pagecount = Request.Params["pagecount"];
        int showcount = Convert.ToInt32(pageindex) * Convert.ToInt32(pagecount);
        string sbr = "";
        string sql = @"select top " + pagecount + @" *
                        from 
                        (
                            select row_number() over(order by paydate desc) as rownumber,
                            a.ShopCode,s.ShopName,convert(varchar(7),paydate,23)as ReconDate,sjpay from KM_CustomerMonthAcc a
                                            inner join v_shop s on s.Shopcode = a.shopcode                                          
                                            inner join Sys_UserDataRights b on b.SourceID = s.stockid
                                            where ClState = " + ClState+ " and userid=" + userid + @"
                        ) A
                        where rownumber > " + showcount.ToString();

        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count == 0)
            {
                sbr = "{\"status\":\"1\",\"msg\":\"没有数据！\"}";
            }
            else
            {
                sbr = DataSetToJson(ds);
            }
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }
    
    private void getmyshopmoney()
    {
        string sbr = "";
        string userid = Request.Params["userid"];
        string bdate = DateTime.Now.ToString("yyyy-MM-dd");
        string sql = @"select s.shopcode,s.shopname,isnull(cast(sum(a.PayAmount) as int),0) as daymoney from v_shop s
                    inner join Sys_UserDataRights b on b.SourceID = s.stockid
                    left join V_PosSale a on s.shopid = a.shopid and cast(convert(varchar(20),a.BillDate,23) as datetime) = '" + bdate + @"'
                    where b.UserID = " + userid+" group by s.shopcode,s.shopname ";

        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {

            sbr = "{\"status\":\"1\","
                + "\"shopcode\":\"" + ds.Tables[0].Rows[0]["shopcode"] + "\","
                + "\"shopname\":\"" + ds.Tables[0].Rows[0]["shopname"] + "\","
                + "\"today\":\"" + DateTime.Now.ToString("yyyy年MM月dd日") + "\","
                + "\"daymoney\":\"" + ds.Tables[0].Rows[0]["daymoney"] + "\""
                +"}";
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }
}